package cn.com.surker.system.service.impl;

import cn.com.surker.material.entity.MainMaterialExaminePrice;
import cn.com.surker.system.entity.SysNetwork;
import cn.com.surker.system.dao.SysNetworkDao;
import cn.com.surker.system.service.SysNetworkService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import cn.com.surker.spring.RestResultDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
* <p>
    * 网络信息记录 服务实现类
    * </p>
*
* @author admin
* @since 2025-06-14
*/
@Service
public class SysNetworkServiceImpl extends ServiceImpl<SysNetworkDao, SysNetwork> implements SysNetworkService {

        @Resource
        private SysNetworkDao networkDao;


        /**
        * 根据条件查询所有的数据
        * @param dto
        * @return
        */
        public RestResultDto findAll(SysNetwork dto){

            QueryWrapper<SysNetwork> query = new QueryWrapper();
            query.setEntity(dto);
            List<SysNetwork> list = networkDao.selectList(query);

            return RestResultDto.success(list);
        }


        /**
        * 分页查询数据
        * @return
        */
        public IPage findByPage(String searchVal, Long orgId, Integer offset, Integer limit){
            PageHelper.offsetPage(offset, limit);
            QueryWrapper<SysNetwork> query = new QueryWrapper();
            query.eq("status", 0);
            if (!StringUtils.isNull(orgId))
                query.eq("org_id", orgId);
            if (!StringUtils.isNull(searchVal))
                query.and(orderWrapper -> orderWrapper.like("org_name", searchVal).or()
                        .like("name", searchVal).or().like("remark", searchVal)
                        .or().like("dept_name", searchVal).or().like("username", searchVal)
                        .or().like("ip_address", searchVal).or().like("ip_out", searchVal)
                        .or().like("gateway", searchVal).or().like("remote", searchVal));
            IPage<SysNetwork> iPage = networkDao.selectPage(new Page<>((offset + limit) /limit, limit), query);
            /**分页插件**/
            if (!StringUtils.isNull(iPage.getRecords())) {
                PageInfo pageInfo = new PageInfo(iPage.getRecords());
                if (!StringUtils.isNull(pageInfo.getTotal()))
                    iPage.setTotal(pageInfo.getTotal());
            }
            return iPage;
        }


        /**
        * 新增数据
        * @param dto page
        * @return
        */
        public RestResultDto saveInit(SysNetwork dto){
            int saveStatus = networkDao.insert(dto);
            if(saveStatus > 0){
                return RestResultDto.success("保存成功!");
            }else{
                return RestResultDto.success("保存失败!");
            }

        }

        /**
        * 更新数据
        * @param dto page
        * @return
        */
        public RestResultDto update(SysNetwork dto){

            int saveStatus = networkDao.updateById(dto);
            if(saveStatus > 0){
                return RestResultDto.success("保存成功!");
            }else{
                return RestResultDto.success("保存失败!");
            }

        }
}